package com.yunchen.server.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.server.mapper.BaseDaoMapper;
import com.yunchen.server.mapper.PermissionsMapper;
import com.yunchen.server.pojo.Permissions;
import com.yunchen.server.pojo.PermissionsExample;
import com.yunchen.server.util.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PermissionsService {
    @Autowired
    private BaseDaoMapper baseDaoMapper;
    @Autowired
    private PermissionsMapper permissionsMapper;
    public Return Add(Permissions permissions) {
        Return re = new Return();
        permissions.setCreatedAt(new Date());
        permissionsMapper.insertSelective(permissions);
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }
    public Return Update(Permissions permissions) {
        Return re = new Return();
        permissionsMapper.updateByPrimaryKeySelective(permissions);
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }
    public Return Del(Permissions permissions) {
        Return re = new Return();
        permissionsMapper.deleteByPrimaryKey(permissions.getPermissionId());
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }
    public JSONArray List (JSONObject json) {
        PermissionsExample permissionsExample=new PermissionsExample();
        permissionsExample.createCriteria().andParentIdEqualTo(0).andPermissionTypeEqualTo(0);
        permissionsExample.setOrderByClause(" sort_order asc");
        List<Permissions> permissions = permissionsMapper.selectByExample(permissionsExample);
        JSONArray array=new JSONArray();
        for (int i=0;i<permissions.size();i++){
            JSONObject jsonObject=new JSONObject();

            jsonObject.put("permissionId",permissions.get(i).getPermissionId());
            jsonObject.put("permissionName",permissions.get(i).getPermissionName());
            jsonObject.put("permissionCode",permissions.get(i).getPermissionCode());
            jsonObject.put("permissionType",permissions.get(i).getPermissionType());
            jsonObject.put("parentId",permissions.get(i).getParentId());
            jsonObject.put("resourcePath",permissions.get(i).getResourcePath());
            jsonObject.put("icon",permissions.get(i).getIcon());
            jsonObject.put("sortOrder",permissions.get(i).getSortOrder());
            jsonObject.put("status",permissions.get(i).getStatus());
            jsonObject.put("createdAt",permissions.get(i).getCreatedAt());
            jsonObject.put("updatedAt",permissions.get(i).getUpdatedAt());
            PermissionsExample permissionsExample1=new PermissionsExample();
            permissionsExample1.createCriteria().andParentIdEqualTo(permissions.get(i).getPermissionId());
            permissionsExample1.setOrderByClause(" permission_type asc,sort_order asc");
            List<Permissions> permissions1 = permissionsMapper.selectByExample(permissionsExample1);
            JSONArray array1=new JSONArray();
            for (int k=0;k<permissions1.size();k++){
                JSONObject jsonObject1=new JSONObject();
                jsonObject1.put("permissionId",permissions1.get(k).getPermissionId());
                jsonObject1.put("permissionName",permissions1.get(k).getPermissionName());
                jsonObject1.put("permissionCode",permissions1.get(k).getPermissionCode());
                jsonObject1.put("permissionType",permissions1.get(k).getPermissionType());
                jsonObject1.put("parentId",permissions1.get(k).getParentId());
                jsonObject1.put("resourcePath",permissions1.get(k).getResourcePath());
                jsonObject1.put("icon",permissions1.get(k).getIcon());
                jsonObject1.put("sortOrder",permissions1.get(k).getSortOrder());
                jsonObject1.put("status",permissions1.get(k).getStatus());
                jsonObject1.put("createdAt",permissions1.get(k).getCreatedAt());
                jsonObject1.put("updatedAt",permissions1.get(k).getUpdatedAt());
                PermissionsExample permissionsExample2=new PermissionsExample();
                permissionsExample2.createCriteria().andParentIdEqualTo(permissions1.get(k).getPermissionId()).andPermissionTypeEqualTo(1);
                permissionsExample2.setOrderByClause("sort_order asc");
                List<Permissions> permissions2 = permissionsMapper.selectByExample(permissionsExample2);
                if (permissions2.size()>0){
                    jsonObject1.put("children",permissions2);
                }

                array1.add(jsonObject1);
            }
            if (array1.size()>0){
                jsonObject.put("children",array1);
            }

            array.add(jsonObject);
        }
        return array;
    }
}
